/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author WY
 */


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;

public class JAVA_grafic extends JFrame
{
    
    
    Lift_State p1;
    Show_Floor p2;
    Button_control p3;
    Draw p4;
    
    int y=550;         
    int floor=0;
    int floor_end=1;
    
    
        public JAVA_grafic()
      {

        this.setTitle("CP2013 JAVA 2---TEAM8");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p1 = new Lift_State ();
        p2 = new Show_Floor();       
        p3 = new Button_control();
        p4 = new Draw();
        
        getContentPane().add(BorderLayout.NORTH, p1);
        getContentPane().add(BorderLayout.WEST, p2);
        getContentPane().add(BorderLayout.EAST, p3);
        getContentPane().add(BorderLayout.CENTER, p4);
        
        
        this.setSize(750, 640);
        this.setResizable(false);
        
        setVisible(true);
      }
        
        public static void main(String[] args)
      {
             new JAVA_grafic();
      }
      
        
        
    class  Draw extends JPanel implements Runnable 
    { private boolean isAlive;
    private boolean isStopped;
        
        
        public  Draw()
    {
            for(int i=0;i<20;i++)
               {
	           p3.jbt[i].addActionListener(new Button_control());
	           p3.jbt[i].setActionCommand(""+(i+1));
	       }
        }
        
        
         public void paintComponent(Graphics g) 
        {
            super.paintComponent(g);
            
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(200,50,200,500);
            if (isStopped)g.setColor(Color.YELLOW);
            else g.setColor(Color.BLACK);
            g.fillRect(200,y,200,10);
            
        }
         
         public void run()
         {
             if (isAlive) {
             return;
         } else {isAlive = true;}
             while(true)
             {
                  if(floor_end<floor)
                   {
	               up();
	           }else if(floor_end>floor)
                   {
	               down();    
	           }else if(floor_end==floor)
                   {
	               stop();
	           }
             
             repaint();
             try
                {
                    Thread.sleep(500);//the speed of the cars moving
                } catch (InterruptedException ex)
                {
                }
             }
         }
            
         
          public void up()
           {
	       p1.setState("Up");
               
               p1.SetNum(floor_end);	     
	       p2.jlb[floor_end].setForeground(Color.GREEN);
	       p2.jlb[floor_end-1].setForeground(Color.BLACK);               
	       floor_end++;
               y-=30;
               isStopped = false;
           }
          
          public void down()
           {
	       p1.setState("Down");              
               p1.SetNum(floor_end);
	       p2.jlb[floor_end-2].setForeground(Color.GREEN);
	       p2.jlb[floor_end-1].setForeground(Color.BLACK);
	       floor_end--;
               y+=30;
               isStopped = false;
	   }
          
           public void stop()
           {
	       if(floor==1)
               {
	           floor_end=0;
	           p2.jlb[0].setForeground(Color.GREEN);
	           p2.jlb[1].setForeground(Color.BLACK);
	           floor_end=1;
	       }
               p1.setState("Arrive");
               
               p1.SetNum(floor_end);
	       isStopped = true;
	       p3.jbt[floor-1].setForeground(Color.BLACK);
	       //runner.stop();
	   }
        
           
        
          
            
    }  
    
    
    
    class Button_control extends JPanel implements ActionListener
    {
        
        JButton jbt[]=new JButton[22];
        JComboBox combo1;
        String[] s = {"1", "2", "3","4","5","6"};
        JLabel l1;
       // JButton Up_Button;
       // JButton Down_Button;
       
	   public Button_control()
           {
	       setLayout(new GridLayout(15,20));
               
	       for(int i=0;i<20;i++)
               {
	           jbt[i]=new JButton("     "+(i+1));
	          add(jbt[i]); 
	       } 
               
               
               jbt[20]=new JButton("up");
               jbt[21]=new JButton("down");
               add(jbt[20]);
               add(jbt[21]);
               
               // Up_Button = new JButton("  up");
              //  Up_Button = new JButton("  down");
                
               // combo1 = new JComboBox(s);
               // combo1.addActionListener(this);
              //  add(Up_Button);
               // add(Down_Button);
                //add(combo1);
                
                
	   }
           
           public void actionPerformed(ActionEvent ae)
               {
                   
                   Thread myThread = new Thread(p4);
	           floor=Integer.parseInt(ae.getActionCommand());
	           jbt[floor-1].setForeground(Color.RED);	          
                   myThread.start();
	       }
        
      }
    
    
    
    
    
    
    class Lift_State extends JPanel
    {
           JLabel state = new JLabel("=====");
	   JLabel num=new JLabel("      =====");
	   
           public Lift_State()
           {
	       //setLayout(new FlowLayout(FlowLayout.CENTER));
	       state.setFont(new Font("",Font.BOLD,20));
	       state.setForeground(Color.RED);
	       num.setFont(new Font("",Font.BOLD,20));
	       num.setForeground(Color.black);
	       add(state);
               add(num);
	    
	   }
           
            public void setState(String s)
           {
	       if(s=="Up")
               {
	           state.setText("UP");
	       }
               if(s=="Down")
               {                  
	           state.setText("DOWN");
	       }
               if(s=="Arrive")
               {
                   state.setText("ARRIVE");
                   
               }
	   }
           
	   public void SetNum(int n)
           {
	       num.setText(" "+"FLOOR   "+n);
	   }
      }
    
    
    class Show_Floor extends JPanel
        { 
	   JLabel jlb[]=new JLabel[20];          
	   public Show_Floor()
           {
	       setLayout(new GridLayout(20,20));
	       for(int i=19;i>=0;i--)
               {
	           jlb[i]=new JLabel(""+(i+1));
	           jlb[i].setFont(new Font("",Font.BOLD,15));
	           add(jlb[i]);
	       }
	   }
	}
    
    }
    
   
        
